package com.zjdiepu.www.manage.api.icbc;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.status.BusiStatus;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.SortFieldType;
import com.zjdiepu.www.base.enums.type.TypeData;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.common.util.DateUtil;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessVoMapper;
import com.zjdiepu.www.ibiz.icbc.IDzApplyDiviService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.icbc.dto.DzApplyDivi;
import com.zjdiepu.www.model.icbc.ext.DzApplyDiviExt;
import com.zjdiepu.www.model.yw.dto.YwBusiness;
import com.zjdiepu.www.model.yw.request.YwBusinessQueryRequest;
import com.zjdiepu.www.security.exception.BasePartnerException;
import com.zjdiepu.www.util.ExceptionHelper;
import com.zjdiepu.www.util.YwExtHelper;

/**
 * dz_申请分期Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年12月13日 18时05分<br/>
 * @since 1.0<br/>
 */
@RestController
@RequestMapping(value= "DzApplyDiviController", produces="application/json;charset=UTF-8")
@Api(tags = {"dz_申请分期"}, produces="application/json;charset=UTF-8")
public class DzApplyDiviController extends BaseController {
    @Resource
    private IDzApplyDiviService dzApplyDiviService;
    @Resource
    private IYwBusinessService ywBusinessService;
    @Resource
    private YwBusinessVoMapper ywBusinessVoMapper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: applyDiviPost  <br/>
     * @Description:补充分期材料<br/>
     * @param ywBusinessQueryRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "补充分期材料",response = Boolean.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "applyDiviPost", method = RequestMethod.POST)
    public ApiResponse applyDiviPost(String busiId) throws BasePartnerException{
    	if(StringUtils.isBlank(busiId)) {
    		ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
    	}
    	dzApplyDiviService.savePostDiviAttachments(busiId);
    	return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: applyDiviPostList  <br/>
     * @Description:补充分期材料<br/>
     * @param ywBusinessQueryRequest
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "补充分期材料列表",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "applyDiviPostList", method = RequestMethod.POST)
    public ApiResponse applyDiviPostList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser sysUser = (SysUser)getCurrentUser();
    	PageVo page = getPage(ywBusinessQueryRequest);
    	ywBusinessQueryRequest.setBaseWhere();
    	ywBusinessQueryRequest.setBankLoan(sysUser.getLoanBank());
    	ywBusinessQueryRequest.setCodeUnits(sysUser.getCodeUnits());
    	ywBusinessQueryRequest.setNeedLimit(true);
    	Date applyDiviTimeStartDate = ywBusinessQueryRequest.getApplyDiviTimeStartDate();
    	if(applyDiviTimeStartDate != null) {
    		ywBusinessQueryRequest.setApplyDiviTimeStart(DateUtil.truncAndAddDays(applyDiviTimeStartDate, 0).getTime() / 1000);
    	} 
    	Date applyDiviTimeEndDate = ywBusinessQueryRequest.getApplyDiviTimeEndDate();
    	if(applyDiviTimeEndDate != null){
    		ywBusinessQueryRequest.setApplyDiviTimeEnd(DateUtil.truncAndAddDays(applyDiviTimeEndDate, 1).getTime() / 1000);
    	}
    	ywBusinessQueryRequest.setMinBusiStatus(BusiStatus.SHEN_PI_WAN_CHENG.getCode()); //审批完成-110（包括）
        ywBusinessQueryRequest.setMaxBusiStatus(BusiStatus.HETONG_CONSTRACT_OK.getCode());//银行已放款-500（不包括）
        ywBusinessQueryRequest.setTypeData(TypeData.HZICBC.getCode());
        ywBusinessQueryRequest.setPostDivi(true);
    	
    	int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
    	page.setRecords(records);
    	if (records > 0) {
    		List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
    		ywBusinessVoMapper.mapper(ywBusinesss);
    		page.setRows(ywBusinesss);
    	} else {
    		page.setRows(CollectionInitUtil.emptyList());
    	}
    	return ApiResponse.success(page);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 申请分期 <br/>
     * @param ywBusinessExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "申请分期信息记录",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "applyDiviHisList", method = RequestMethod.POST)
    public ApiResponse applyDiviHisList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser sysUser = (SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(sysUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(sysUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        Date applyDiviTimeStartDate = ywBusinessQueryRequest.getApplyDiviTimeStartDate();
        if(applyDiviTimeStartDate != null) {
        	ywBusinessQueryRequest.setApplyDiviTimeStart(DateUtil.truncAndAddDays(applyDiviTimeStartDate, 0).getTime() / 1000);
        } 
       Date applyDiviTimeEndDate = ywBusinessQueryRequest.getApplyDiviTimeEndDate();
        if(applyDiviTimeEndDate != null){
        	ywBusinessQueryRequest.setApplyDiviTimeEnd(DateUtil.truncAndAddDays(applyDiviTimeEndDate, 1).getTime() / 1000);
        }
        ywBusinessQueryRequest.setDiviHis(true);
        
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 申请分期 <br/>
     * @param ywBusinessExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "申请分期信息",response = YwBusiness.class, httpMethod="POST", tags={"多表查询"})
    @RequestMapping(value = "applyDiviList", method = RequestMethod.POST)
    public ApiResponse applyDiviList(@Validated @ModelAttribute YwBusinessQueryRequest ywBusinessQueryRequest){
    	SysUser sysUser = (SysUser)getCurrentUser();
        PageVo page = getPage(ywBusinessQueryRequest);
        ywBusinessQueryRequest.setBaseWhere();
        ywBusinessQueryRequest.setBankLoan(sysUser.getLoanBank());
        ywBusinessQueryRequest.setCodeUnits(sysUser.getCodeUnits());
        ywBusinessQueryRequest.setNeedLimit(true);
        YwExtHelper.initExt(ywBusinessQueryRequest, SortFieldType.SUBMIT_TIME);
//        合同已套打（460），并且 type_data =  3
        ywBusinessQueryRequest.addBusiStatus(BusiStatus.HETONG_CONSTRACT_OK.getCode());
        ywBusinessQueryRequest.setTypeData(TypeData.HZICBC.getCode());
        ywBusinessQueryRequest.setApplyDivi(true);
        ywBusinessQueryRequest.setWhereSql(" AND (DAD.`APPLY_DIVI_STATUS` IS NULL OR  DAD.`APPLY_DIVI_STATUS` = 10 OR DAD.`APPLY_DIVI_STATUS` = 50 OR DAD.`APPLY_DIVI_STATUS` = 9000)");
        
        int records = ywBusinessService.countMultiByExt(ywBusinessQueryRequest);
        page.setRecords(records);
        if (records > 0) {
            List<YwBusiness> ywBusinesss = ywBusinessService.selectMultiByExt(ywBusinessQueryRequest);
            ywBusinessVoMapper.mapper(ywBusinesss);
            page.setRows(ywBusinesss);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: applyDivi  <br/>
     * @Description: 申请分期 <br/>
     * @param applyDivi
     * @return
     * @version V1.0  <br/>
     * @throws BasePartnerException 
     */
    @ApiOperation(value = "申请分期", response = Boolean.class, httpMethod="POST")
    @RequestMapping(value = "applyDivi", method = RequestMethod.POST)
    public ApiResponse applyDivi(@Validated(Add.class) @ModelAttribute DzApplyDivi dzApplyDivi) throws BasePartnerException {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzApplyDivi.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzApplyDivi.setModifierName(currentUser.getName());
        dzApplyDiviService.saveApplyDivi(dzApplyDivi);
        return ApiResponse.success(dzApplyDivi.getId());
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: save  <br/>
     * @Description: 新增dz_申请分期 <br/>
     * @param dzApplyDivi
     * @return
     * @version V1.0  <br/>
     * @throws BasePartnerException 
     */
    @ApiOperation(value = "新增dz_申请分期", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class) @ModelAttribute DzApplyDivi dzApplyDivi) throws BasePartnerException {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzApplyDivi.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzApplyDivi.setModifierName(currentUser.getName());
        dzApplyDiviService.saveUpdateAndDelete(dzApplyDivi, OperateType.add);
        return ApiResponse.success(dzApplyDivi.getId());
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: remove  <br/>
     * @Description: 删除dz_申请分期<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除dz_申请分期", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzApplyDiviService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.DELETED.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: disable  <br/>
     * @Description: 禁用dz_申请分期<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用dz_申请分期", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzApplyDiviService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.INVALID.getCode(), ids);
        return ApiResponse.success(true);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: enable <br/>
     * @Description: 启用dz_申请分期<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用dz_申请分期", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	AbstractSysUser currentUser = getCurrentUser();
    	List<String> ids = Arrays.asList(id.split(","));
    	dzApplyDiviService.updateBatch(currentUser.getId(), currentUser.getName(), DpStatus.NORMAL.getCode(), ids);
        return ApiResponse.success(true);
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: modify  <br/>
     * @Description: 修改dz_申请分期 <br/>
     * @param dzApplyDivi
     * @return
     * @version V1.0  <br/>
     * @throws BasePartnerException 
     */
    @ApiOperation(value = "修改dz_申请分期", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class) @ModelAttribute DzApplyDivi dzApplyDivi) throws BasePartnerException {
    	AbstractSysUser currentUser = getCurrentUser();
    	dzApplyDivi.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dzApplyDivi.setModifierName(currentUser.getName());
    	dzApplyDiviService.saveUpdateAndDelete(dzApplyDivi, OperateType.edit);
        return ApiResponse.success(dzApplyDivi.getId());
    }

    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询dz_申请分期<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询dz_申请分期", response = DzApplyDivi.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DzApplyDivi dzApplyDivi = dzApplyDiviService.selectById(id);
       if(null == dzApplyDivi) {
    	   return ApiResponse.fail(ResultFlagType.MISSING);
       }
       return ApiResponse.success(dzApplyDivi);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: list  <br/>
     * @Description: 条件查询dz_申请分期 <br/>
     * @param dzApplyDiviExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询dz_申请分期",response = DzApplyDivi.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DzApplyDiviExt dzApplyDiviExt){
        dzApplyDiviExt.notDeleted();
        PageVo page = getPage(dzApplyDiviExt);
        int records = dzApplyDiviService.countByExt(dzApplyDiviExt);
        page.setRecords(records);
        if (records > 0) {
            List<DzApplyDivi> dzApplyDivis = dzApplyDiviService.selectByExt(dzApplyDiviExt);
            page.setRows(dzApplyDivis);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

}
